import 'dart:async';

import 'package:aichat_flutter_wallet/module_common/widgets/button/public_button.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/header/page_assets_header.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/image/public_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lottie/lottie.dart';

import '../../../kai_print.dart';
import '../../../module_common/animate/fade_in_down_animation.dart';
import '../../../module_common/bridge/native_channel_bridge.dart';
import '../../../module_common/utils/util.dart';
import '../../../module_common/widgets/public_tip/public_tip.dart';
import '../../module_create_wallet/pages/import_wallet_page.dart';
import '../../module_create_wallet/pages/page_under_creation.dart';

class NoWalletPage extends StatefulWidget {
  final Function(String)? popCallBack;

  const NoWalletPage({super.key, this.popCallBack});

  @override
  State<NoWalletPage> createState() => _NoWalletPageState();
}

class _NoWalletPageState extends State<NoWalletPage> with SingleTickerProviderStateMixin, WidgetsBindingObserver {
  late final AnimationController lottieController;
  bool _isNodeVisible = false;

  bool _isNodeVisible3 = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    lottieController = AnimationController(vsync: this);
    _delayedShow();
  }

  @override
  void dispose() {
    // _refreshController.headerMode!.removeListener(_refreshListener);
    lottieController.dispose();
    WidgetsBinding.instance.removeObserver(this);

    super.dispose();
  }

  void _delayedShow() async {
    try {
      await Future.delayed(Duration(milliseconds: 1000));
      if (mounted)
        setState(() {
          _isNodeVisible = true;
        });
      // await Future.delayed(Duration(milliseconds: 300));
      // if (mounted)
      //   setState(() {
      //     _isNodeVisible2 = true;
      //   });
      await Future.delayed(Duration(milliseconds: 300));
      if (mounted)
        setState(() {
          _isNodeVisible3 = true;
        });
    } catch (e) {
      kaiPrint(e);
    }
  }

  void resetAnimalForward() async {
    try {
      if (mounted)
        setState(() {
          _isNodeVisible = false;
          // _isNodeVisible2 = false;
          _isNodeVisible3 = false;
        });
      await Future.delayed(Duration(milliseconds: 600));
      if (mounted)
        setState(() {
          _isNodeVisible = true;
        });
      // await Future.delayed(Duration(milliseconds: 300));
      // if (mounted)
      //   setState(() {
      //     _isNodeVisible2 = true;
      //   });
      await Future.delayed(Duration(milliseconds: 300));
      if (mounted)
        setState(() {
          _isNodeVisible3 = true;
        });
    } catch (e) {
      kaiPrint('error: $e');
    }
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) async {
    super.didChangeAppLifecycleState(state);
    if (state == AppLifecycleState.resumed) {
      // 应用程序进入前台
      resetAnimalForward();
    }
  }

  @override
  Widget build(BuildContext context) {
    final double statusBarHeight = MediaQuery.of(context).padding.top;
    return Scaffold(
      appBar: PageAssetsHeaderCom(
          WidgetLeft: InkWell(
            splashColor: Colors.transparent,
            onTap: () {
              NativeChannelBridge.instance.closeFlutterPage(num:1);
            },
            child: Container(
              child: Image.asset(
                'assets/images/integral/arrow_left.png',
                width: 20.w,
                height: 20.w,
              ),
              padding: EdgeInsets.only(
                  left: 15.w, right: 15.w, top: 12.w, bottom: 12.w),
            ),
          ),
        WidgetRight: Container(
          padding: EdgeInsets.only(right: 20.w, top: 9.w, bottom: 9.w),
          alignment: Alignment.centerLeft,
          height: 44.w,
          child: PublicImage(
            image: 'assets/images/create_wallet/logo.png',
            width: 48.w,
            height: 23.w,
          ),
        ),
      ),
      backgroundColor: Colors.white,
      body: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              height: 370.w,
              alignment: Alignment.center,
              child: Lottie.asset(
                'assets/svg_json/empty_wallet3.json',
                controller: lottieController,
                fit: BoxFit.contain,
                repeat: false,
                onLoaded: (composition) {
                  // Configure the AnimationController with the duration of the
                  // Lottie file and start the animation.
                  lottieController
                    ..duration = composition.duration
                    ..repeat();
                },
              ),
            ),
            SizedBox(
              height: 19.w,
            ),
            Container(
              height: 56.w,
              padding: EdgeInsets.only(left: 20.w, right: 20.w, top: 0.w),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Container(
                    alignment: Alignment.center,
                    child: _isNodeVisible
                        ? FadeInDownAnimation(
                            duration: Duration(milliseconds: 500),
                            child: PublicImage(
                              width: 232.w,
                              height: 24.w,
                              image: 'assets/images/create_wallet/Never.gif',
                            ))
                        : null,
                  ),
                  SizedBox(
                    height: 8.w,
                  ),
                  Container(
                      alignment: Alignment.center,
                      child: _isNodeVisible3
                          ? FadeInDownAnimation(
                              duration: Duration(milliseconds: 500),
                              child: Text(
                                'You deserve value from socializing',
                                softWrap: true,
                                overflow: TextOverflow.visible,
                                style: TextStyle(
                                    fontSize: 16.sp, fontWeight: FontWeight.w500, color: Color.fromRGBO(51, 51, 51, 1)),
                              ))
                          : null),
                ],
              ),
            ),
            SizedBox(height: 56.w),
            PublicButton(
                radius:10.w,
                height: 46.w,
                text: 'Create My MPC Wallet',
                type: ButtonType.private,
                onClick: () async {
                  String? serverKey = (await WalletCheck.getServerKey()).$1;
                  if(serverKey.isNotEmpty){
                  Toast.show(context, status: '-1', message: 'Loading for you, please wait a moment.');
                  return ;
                  }
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => PageUnderCreation()),
                  ).then((value) {
                    resetAnimalForward();
                    if (widget.popCallBack != null) widget.popCallBack!(value);
                  });
                }),
            // SizedBox(
            //   height: 15.w,
            // ),
            // PublicButton(
            //     radius:10.w,
            //     height: 46.w,
            //     text: 'Import Recovery Phrase for Wallet',
            //     type: ButtonType.private,
            //     onClick: () async {
            //       String? serverKey = (await WalletCheck.getServerKey()).$1;
            //       if(serverKey.isNotEmpty){
            //         Toast.show(context, status: '-1', message: 'Loading for you, please wait a moment.');
            //         return ;
            //       }
            //       Navigator.of(context)
            //           .push(MaterialPageRoute(
            //               builder: (context) => ImportWalletPage(type: ImportWalletPageType.ImportMPCWallet)))
            //           .then((value) {
            //         resetAnimalForward();
            //         if (widget.popCallBack != null) widget.popCallBack!(value == null ? 'Cancel' : value);
            //       });
            //     }),
            SizedBox(
              height: 62.w,
            ),
          ],
        ),
      ),
    );
  }
}
